﻿using System;
using System.Collections.Generic;
using System.Text;
using Kooboo.CMS.Content.Models;
using System.Web;

namespace KB
{
    public class ResponseFileUtility
    {
        public static void ResponseFile(object content, FileType fileType, string fileName = null)
        {
            if (fileName.IsNullOrEmpty())
            {
                fileName = DateTime.Now.ToShortDateString();
            }
            var contentType = "";
            switch (fileType)
            {
                case FileType.Excel:
                    fileName = fileName + ".xls";
                    contentType = "application/ms-excel";
                    break;
                case FileType.Word:
                    fileName = fileName + ".doc";
                    contentType = "application/msword";
                    break;
                default:
                     fileName = fileName + ".html";
                     contentType = "text/html";
                    break;
            }
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.Charset = "UTF-8";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            HttpContext.Current.Response.ContentType = contentType;
            HttpContext.Current.Response.Write(content);
            HttpContext.Current.Response.End();
        }
        public enum FileType
        {
            Excel,
            HTML,
            Word
        }
    }
}
